/**********************************************************
 * ctcs_init.c
 * Date:
 **********************************************************/
/**********************************************************
 *
 * Header file
 *
 **********************************************************/
#include "sal.h"
#include "dal.h"
#include "ctc_debug.h"
#include "ctcs_api.h"
#include "ctc_chip.h"
#include "ctc_packet.h"
#include "ctc_init.h"
#include "ctc_warmboot.h"
/**********************************************************
 *
 * Defines and macros
 *
 **********************************************************/



/**********************************************************
 *
 * Global and Declaration
 *
 **********************************************************/
ctc_init_cfg_t* g_ctcs_init_cfg[CTC_MAX_LOCAL_DEV_NUM] = {NULL};
extern dal_op_t g_dal_op;

int32 ctcs_sdk_clear_global_cfg(uint8 ldev)
{
    uint8 tmp_ldev;
    ctc_init_cfg_t* p_init_cfg;

    if (ldev >= CTC_MAX_LOCAL_DEV_NUM)
    {
        return CTC_E_INVALID_CHIP_ID;
    }
    if(!g_ctcs_init_cfg[ldev])
    {
        return CTC_E_NONE;
    }
    p_init_cfg = g_ctcs_init_cfg[ldev];
    #define FREE_GLOBAL_CFG(ptr) \
        {\
            if(ptr)\
            {\
                mem_free(ptr);\
                ptr = NULL;\
            }\
        }\

    FREE_GLOBAL_CFG(p_init_cfg->p_chip_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_datapath_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_nh_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_qos_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_vlan_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_l3if_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_parser_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_port_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_linkagg_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_pkt_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_l2_fdb_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_stats_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_acl_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_ipuc_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_mpls_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_learning_aging_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_oam_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_ptp_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_intr_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_stacking_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_dma_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_bpe_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_npm_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_overlay_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->p_ipifx_cfg);
    FREE_GLOBAL_CFG(p_init_cfg->dal_cfg);

    for(tmp_ldev=0; tmp_ldev < CTC_MAX_LOCAL_DEV_NUM; tmp_ldev++)
    {
        FREE_GLOBAL_CFG(p_init_cfg->phy_mapping_para[tmp_ldev]);
    }

    FREE_GLOBAL_CFG(p_init_cfg->ftm_info.key_info);
    FREE_GLOBAL_CFG(p_init_cfg->ftm_info.tbl_info);

    FREE_GLOBAL_CFG(g_ctcs_init_cfg[ldev]);

    return CTC_E_NONE;
}
int32
ctcs_sdk_copy_global_cfg(uint8 ldev, ctc_init_cfg_t * p_cfg)
{
    uint8 tmp_ldev;
    ctc_init_cfg_t* p_init_cfg;

    CTC_PTR_VALID_CHECK(p_cfg);
    if (ldev >= CTC_MAX_LOCAL_DEV_NUM)
    {
        return CTC_E_INVALID_CHIP_ID;
    }
    if (g_ctcs_init_cfg[ldev])
    {
        return CTC_E_NONE;
    }
    g_ctcs_init_cfg[ldev] = mem_malloc(MEM_SYSTEM_MODULE, sizeof(ctc_init_cfg_t));
    if(NULL == g_ctcs_init_cfg[ldev])
    {
        return CTC_E_NO_MEMORY;
    }
    sal_memset(g_ctcs_init_cfg[ldev], 0, sizeof(ctc_init_cfg_t));
    p_init_cfg = g_ctcs_init_cfg[ldev];

    p_init_cfg->init_flag = p_cfg->init_flag;
    sal_memcpy(&p_init_cfg->ftm_info, &p_cfg->ftm_info, sizeof(ctc_ftm_profile_info_t));
    p_init_cfg->ftm_info.key_info = NULL;
    p_init_cfg->ftm_info.tbl_info = NULL;
    p_init_cfg->ftm_info.key_info = (ctc_ftm_key_info_t*)mem_malloc(MEM_SYSTEM_MODULE, sizeof(ctc_ftm_key_info_t)*CTC_FTM_KEY_TYPE_MAX);
    if(!p_init_cfg->ftm_info.key_info)
    {
        goto error_0;
    }
    p_init_cfg->ftm_info.tbl_info = (ctc_ftm_tbl_info_t*)mem_malloc(MEM_SYSTEM_MODULE, sizeof(ctc_ftm_tbl_info_t)*CTC_FTM_TBL_TYPE_MAX);
    if(!p_init_cfg->ftm_info.tbl_info)
    {
        goto error_0;
    }
    sal_memcpy(p_init_cfg->ftm_info.tbl_info, p_cfg->ftm_info.tbl_info, sizeof(ctc_ftm_tbl_info_t)*CTC_FTM_TBL_TYPE_MAX);
    sal_memcpy(p_init_cfg->ftm_info.key_info, p_cfg->ftm_info.key_info, sizeof(ctc_ftm_key_info_t)*CTC_FTM_KEY_TYPE_MAX);
    sal_memcpy(p_init_cfg->gchip, p_cfg->gchip, sizeof(p_cfg->gchip));
    p_init_cfg->local_chip_num = p_cfg->local_chip_num;

    #define MALLOC_AND_COPY_MEM(dest, source, field, type) \
        do{\
            if(source->field)\
            {\
                dest->field = (type*)mem_malloc(MEM_SYSTEM_MODULE, sizeof(type));\
                if(NULL == dest->field)\
                    goto error_0;\
                sal_memcpy(dest->field, source->field, sizeof(type));\
            }\
        }while(0)

    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_chip_cfg, ctc_chip_global_cfg_t);
    p_init_cfg->p_datapath_cfg = (ctc_datapath_global_cfg_t*)mem_malloc(MEM_SYSTEM_MODULE, CTC_MAX_LOCAL_DEV_NUM*sizeof(ctc_datapath_global_cfg_t));
    if(NULL == p_init_cfg->p_datapath_cfg)
    {
        goto error_0;
    }
    sal_memcpy(p_init_cfg->p_datapath_cfg, p_cfg->p_datapath_cfg, CTC_MAX_LOCAL_DEV_NUM*sizeof(ctc_datapath_global_cfg_t));
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_nh_cfg, ctc_nh_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_qos_cfg, ctc_qos_global_cfg_t);

    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_vlan_cfg, ctc_vlan_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_l3if_cfg, ctc_l3if_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_parser_cfg, ctc_parser_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_port_cfg, ctc_port_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_linkagg_cfg, ctc_linkagg_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_pkt_cfg, ctc_pkt_global_cfg_t);

    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_l2_fdb_cfg, ctc_l2_fdb_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_stats_cfg, ctc_stats_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_acl_cfg, ctc_acl_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_ipuc_cfg, ctc_ipuc_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_mpls_cfg, ctc_mpls_init_t);

    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_learning_aging_cfg, ctc_learn_aging_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_oam_cfg, ctc_oam_global_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_ptp_cfg, ctc_ptp_global_config_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_intr_cfg, ctc_intr_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_stacking_cfg, ctc_stacking_glb_cfg_t);


    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_dma_cfg, ctc_dma_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_bpe_cfg, ctc_bpe_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_npm_cfg, ctc_npm_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_overlay_cfg, ctc_overlay_tunnel_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, p_ipifx_cfg, ctc_ipfix_global_cfg_t);
    MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, dal_cfg, dal_op_t);

    for(tmp_ldev=0; tmp_ldev < CTC_MAX_LOCAL_DEV_NUM; tmp_ldev++)
    {
        MALLOC_AND_COPY_MEM(p_init_cfg, p_cfg, phy_mapping_para[tmp_ldev], ctc_chip_phy_mapping_para_t);
    }

    return CTC_E_NONE;
error_0:
    ctcs_sdk_clear_global_cfg(ldev);
    return CTC_E_NO_MEMORY;
}

int32
_ctcs_sdk_map_ldev(uint8 ldev)
{
    dal_ldev_info_t ldev_info;
    int32 ret = 0;
    uint8 index = 0;
    
    sal_memset(&ldev_info, 0, sizeof(dal_ldev_info_t));
    ret = dal_get_ldev_info(ldev, &ldev_info);
    if (ret < CTC_E_NONE)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "dal_get_ldev_info failed:%s@%d \n",  __FUNCTION__, __LINE__);
        return ret;
    }

    for (index = 0; index < ldev_info.pp_num; index++)
    {
        g_ctcs_map_lchip_base[ldev_info.lchip+index] = ldev_info.lchip;
        /* convert lchip to lchip idx */
        g_ctcs_map_ldev[ldev_info.lchip+index] = ldev;   /*temply lchip means ldev*/
    }
    /* convert ldev to lchip */
    g_ctcs_map_lchip[ldev] = ldev_info.lchip;
    g_ctcs_map_pp_num[ldev] = ldev_info.pp_num;
    
    return CTC_E_NONE;
}

int32
_ctcs_sdk_init(uint8 ldev, void * p_global_cfg)
{
    int32  ret = CTC_E_NONE;
    uint8 do_init = 0;
    ctc_init_cfg_t * p_cfg = p_global_cfg;
    ctc_chip_device_info_t device_info;
    dal_op_t dal_cfg;
    uint8 wb_status = 0;
#ifndef CTC_SHELL_DEBUG_ON   
    char sdk_work_platform[2][32] = {"hardware platform", " software simulation platform"};
#endif
    CTC_PTR_VALID_CHECK(p_cfg);

    if (p_cfg->dal_cfg)
    {
        sal_memcpy(&dal_cfg, p_cfg->dal_cfg, sizeof(dal_op_t));
    }
    else
    {
        sal_memcpy(&dal_cfg, &g_dal_op, sizeof(dal_op_t));
    }
    dal_cfg.ldev = ldev;
    ret = dal_op_init(&dal_cfg);
    if (ret != 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "dal_op_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* create lchip & ldev mapping hear */
    ret = _ctcs_sdk_map_ldev(ldev);
    if (ret != 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "_ctcs_sdk_map_ldev failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }
	/* wb init*/
    ret = ctc_warmboot_init(ldev, p_cfg->p_wb_config);
    if (ret != 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "warmboot_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /*install api  */
    if (ctcs_get_chip_type(ldev) != CTC_CHIP_NONE)
    {
        return ret;
    }
    ret = ctcs_install_api(ldev);
    if (ret != 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_install_api failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* chip init*/
    ret = ctcs_chip_init(ldev, p_cfg->local_chip_num);
    if (ret < CTC_E_NONE)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_chip_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }
	
	
    /* init diag */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_DIAG, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_diag_init(ldev, NULL);
        if(CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_diag_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    ret = ctcs_set_gchip_id(ldev, p_cfg->gchip[ldev]);
    CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ldev:%d gchip:%d\n", ldev, p_cfg->gchip[ldev]);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_set_gchip_id failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* dal init */
#ifdef _SAL_LINUX_UM
    ret = dal_set_device_access_type(DAL_PCIE_MM);
    if (ret < 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Failed to register dal callback\r\n");
        return ret;
    }
#endif

#ifdef EMULATION_ENV
		extern int32 ctc_master_cli(int32 ctc_shell_mode);
		ctc_master_cli(0);
#endif

    /* ftm alloc */
    ret = ctcs_ftm_mem_alloc(ldev, &(p_cfg->ftm_info));
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_ftm_alloc failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    ret = ctcs_chip_get_property(ldev, CTC_CHIP_PROP_DEVICE_INFO, (void*)&device_info);
    if (ret != 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_chip_get_property failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }
    CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Chip name: %s, Device Id: %d, Version Id: %d \n", device_info.chip_name, device_info.device_id, device_info.version_id);

     CTC_INIT_MAPPING_FEATURE(p_cfg->init_flag, &(p_cfg->p_chip_cfg->feature_en));
    /* the following is resource module */
    /* chip global config */
    ret = ctcs_set_chip_global_cfg(ldev, p_cfg->p_chip_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_set_chip_global_cfg failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* datapath init */
    ret = ctcs_datapath_init(ldev, p_cfg->p_datapath_cfg + ldev);
    if (ret != 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_datapath_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }
#ifndef CTC_SHELL_DEBUG_ON  
    CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "SDK_WORK_PLATFORM:%s \r\n local_chip_num:%d \r\n",
                    sdk_work_platform[SDK_WORK_PLATFORM], p_cfg->local_chip_num);
#endif  
    /* initializate common table/register */
    ret = ctcs_register_init(ldev, NULL);
    if (ret != 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_register_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* xdata init */
    ret = ctcs_xdata_init(ldev, NULL);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_xdata_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* interrupt init */
    ret = ctcs_interrupt_init(ldev, p_cfg->p_intr_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_interrupt_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* init dma */
    ret = ctcs_dma_init(ldev, p_cfg->p_dma_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_dma_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* flexe init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_FLEXE, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_flexe_init(ldev, NULL);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctc_flexe_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }
   /* aps init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_APS, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_aps_init(ldev, NULL);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_aps_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    /* nexthop */
    ret = ctcs_nexthop_init(ldev, p_cfg->p_nh_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_nexthop_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* l3if */
    ret = ctcs_l3if_init(ldev, p_cfg->p_l3if_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_l3if_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* stats init */
    ret = ctcs_stats_init(ldev, p_cfg->p_stats_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_stats_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* qos */
    ret = ctcs_qos_init(ldev, p_cfg->p_qos_cfg);
    if (ret != 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_qos_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* learning aging init */
    ret = ctcs_learning_aging_init(ldev, p_cfg->p_learning_aging_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_learning_aging_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* SCL */
    ret = ctcs_scl_init(ldev, NULL);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_scl_init failed: %s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* VLAN */
    ret = ctcs_vlan_init(ldev, p_cfg->p_vlan_cfg);
    if (ret != 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_vlan_init failed: %s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /*parser*/
    ret = ctcs_parser_init(ldev, p_cfg->p_parser_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_parser_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /*    port  */
    ret = ctcs_port_init(ldev, p_cfg->p_port_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_port_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    if (p_cfg->phy_mapping_para[ldev] && (SDK_WORK_PLATFORM == 0))
    {
        /* must be done before init phy */
        ret = ctcs_chip_set_property(ldev, CTC_CHIP_PROP_PHY_MAPPING, p_cfg->phy_mapping_para[ldev]);
        if (ret != 0)
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_chip_set_property failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    ret = ctcs_internal_port_init(ldev, NULL);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_internal_port_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* Linkagg */
    ret = ctcs_linkagg_init(ldev, p_cfg->p_linkagg_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_linkagg_init failed: %s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    ret = ctcs_pdu_init(ldev, NULL);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_pdu_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    ret = ctcs_packet_init(ldev, p_cfg->p_pkt_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_pdu_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /*the following is feature module*/
    /*mirror*/
    ret = ctcs_mirror_init(ldev, NULL);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_mirror_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }


    /* l2 init */
    ret = ctcs_l2_fdb_init(ldev, p_cfg->p_l2_fdb_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_l2_fdb_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* stp init */
    ret = ctcs_stp_init(ldev, NULL);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_stp_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* acl init */
    ret = ctcs_acl_init(ldev, p_cfg->p_acl_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_acl_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }


    /* ipuc init */
    ret = ctcs_ipuc_init(ldev, p_cfg->p_ipuc_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_ipuc_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }


    /* mpls init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_MPLS, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_mpls_init(ldev, p_cfg->p_mpls_cfg);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_mpls_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    /* ipmc init */
    ret = ctcs_ipmc_init(ldev, NULL);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_ipmc_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* security init */
    ret = ctcs_security_init(ldev, p_cfg->p_security_cfg);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_security_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* oam init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_OAM, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_oam_init(ldev, p_cfg->p_oam_cfg);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_oam_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }


    /**ptp init*/
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_PTP, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_ptp_init(ldev, p_cfg->p_ptp_cfg);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_ptp_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        }
    }


    /* SyncE init*/
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_SYNCE, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_sync_ether_init(ldev, NULL);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_sync_ether_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    /* stacking init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_STACKING, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_stacking_init(ldev, p_cfg->p_stacking_cfg);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_stacking_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }


    /* bpe init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_BPE, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_bpe_init(ldev, p_cfg->p_bpe_cfg);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_bpe_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }
    

#if (FEATURE_MODE == 0)
    /* Ipfix init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_IPFIX, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_ipfix_init(ldev, p_cfg->p_ipifx_cfg);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_ipfix_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }
#endif

    /* Monitor init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_MONITOR, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_monitor_init(ldev, NULL);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_monitor_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }


    /* Overlay init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_OVERLAY, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_overlay_tunnel_init(ldev, p_cfg->p_overlay_cfg);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_overlay_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }


    /* Efd init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_EFD, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_efd_init(ldev, NULL);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_efd_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    /* Trill init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_TRILL, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_trill_init(ldev, NULL);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_trill_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    /* Fcoe init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_FCOE, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_fcoe_init(ldev, NULL);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_fcoe_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    if (ret == CTC_E_NOT_SUPPORT)
    {
        ret =  CTC_E_NONE;
    }

#if (FEATURE_MODE == 0)
    /* Wlan init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_WLAN, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_wlan_init(ldev, p_cfg->p_wlan_cfg);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_wlan_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    /* dot1ae init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_DOT1AE, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_dot1ae_init(ldev, NULL);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_dot1ae_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    /* Npm init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_NPM, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_npm_init(ldev, (void*)(p_cfg->p_npm_cfg));
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_npm_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    /* srv6 init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_SRV6, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_srv6_init(ldev, p_cfg->p_srv6_cfg);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_srv6_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    /* dtel init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_DTEL, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_dtel_init(ldev, p_cfg->p_dtel_cfg);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_dtel_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    /* SC OAM init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_SC_OAM, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_sc_oam_init(ldev, NULL);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_sc_oam_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }

    /* pon init */
    CTC_INIT_DO_INIT(CTC_INIT_MODULE_PON, do_init, p_cfg->init_flag);
    if (do_init)
    {
        ret = ctcs_pon_init(ldev, NULL);
        if (CTC_INIT_ERR_RET(ret))
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_pon_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
            return ret;
        }
    }
#endif

    wb_status = CTC_WB_STATUS(CTC_MAP_LCHIP(ldev)) ;
    ret = ctc_warmboot_init_done(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
         CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctc_wb_init_done:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
         return ret;
    }

    /* warm boot ,must be the last */
    if (wb_status == CTC_WB_STATUS_RELOADING)
    {
        ret = ctcs_dma_init(ldev, p_cfg->p_dma_cfg);
        if (ret != CTC_E_NONE)
        {
            CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_dma_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));

            return ret;
        }
    }

    ret = ctcs_chip_init_done(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_chip_init failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    if (ret == CTC_E_NOT_SUPPORT)
    {
        ret =  CTC_E_NONE;
    }

    return ret;
}

int32
_ctcs_sdk_deinit(uint8 ldev)
{
    int32 ret = 0;

    /* pon deinit */
    ret = ctcs_pon_deinit(ldev, NULL);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_pon_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* chip deinit must be put at the beginning after pon deinit*/
    ret = ctcs_chip_deinit(ldev);
    if (ret < CTC_E_NONE)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_chip_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* diag deinit */
    ret = ctcs_diag_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctc_diag_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    ret = ctcs_l2_fdb_deinit(ldev);
    if (ret < 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_l2_fdb_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* isr deinit */
    ret = ctcs_interrupt_deinit(ldev);
    if (ret < 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"interrupt deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* dma deinit */
    ret = ctcs_dma_deinit(ldev);
    if (ret < 0)
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"dma deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

#if (FEATURE_MODE == 0)
    /* wlan deinit */
    ret = ctcs_wlan_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_wlan_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* npm deinit */
    ret = ctcs_npm_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_npm_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* dot1ae deinit */
    ret = ctcs_dot1ae_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_dot1ae_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* srv6 deinit */
    ret = ctcs_srv6_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_srv6_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* dtel deinit */
    ret = ctcs_dtel_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_dtel_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* flexe deinit */
    ret = ctcs_flexe_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_flexe_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* sc oam deinit */
    ret = ctcs_sc_oam_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctc_sc_oam_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }
#endif
    /* fcoe deinit */
    ret = ctcs_fcoe_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_fcoe_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* trill deinit */
    ret = ctcs_trill_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_trill_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* efd deinit */
    ret = ctcs_efd_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_efd_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* overlay tunnel deinit */
    ret = ctcs_overlay_tunnel_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_overlay_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* monitor deinit */
    ret = ctcs_monitor_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_monitor_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* ipfix deinit */
    ret = ctcs_ipfix_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_ipfix_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* bpe deinit */
    ret = ctcs_bpe_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_bpe_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* stacking deinit */
    ret = ctcs_stacking_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_stacking_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* sync ether deinit */
    ret = ctcs_sync_ether_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_sync_ether_init failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* ptp deinit */
    ret = ctcs_ptp_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_ptp_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
    }

    /* oam deinit */
    ret = ctcs_oam_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_oam_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* security deinit */
    ret = ctcs_security_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_security_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* aps deinit */
    ret = ctcs_aps_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_aps_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* learning aging deinit */
    ret = ctcs_learning_aging_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_learning_aging_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* ipmc deinit */
    ret = ctcs_ipmc_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_ipmc_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* mpls deinit */
    ret = ctcs_mpls_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_mpls_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* ipuc deinit */
    ret = ctcs_ipuc_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_ipuc_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* acl deinit */
    ret = ctcs_acl_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_acl_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* stats deinit */
    ret = ctcs_stats_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_stats_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* stp deinit */
    ret = ctcs_stp_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_stp_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /*mirror deinit */
    ret = ctcs_mirror_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_mirror_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* packet deinit*/
    ret = ctcs_packet_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_packet_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* pdu deinit*/
    ret = ctcs_pdu_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_pdu_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* linkagg deinit*/
    ret = ctcs_linkagg_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_linkagg_deinit failed: %s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* internal port deinit */
    ret = ctcs_internal_port_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_internal_port_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* port deinit */
    ret = ctcs_port_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_port_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* parser deinit */
    ret = ctcs_parser_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_parser_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* l3if deinit */
    ret = ctcs_l3if_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_l3if_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* vlan deinit */
    ret = ctcs_vlan_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_vlan_deinit failed: %s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* scl deinit */
    ret = ctcs_scl_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_scl_deinit failed: %s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* qos deinit */
    ret = ctcs_qos_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_qos_deinit failed:%s@%d %s\n", __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* nexthop deinit */
    ret = ctcs_nexthop_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_nexthop_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* register deinit */
    ret = ctcs_register_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_register_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* xdata deinit */
    ret = ctcs_xdata_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP,"ctcs_xdata_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* datapath deinit */
    ret = ctcs_datapath_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_datapath_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* ftm free */
    ret = ctcs_ftm_mem_free(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_ftm_free failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* uninstall api  */
    ret = ctcs_uninstall_api(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "ctcs_uninstall_api failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    /* dal deinit */
    ret = dal_op_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "dal_op_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }
	
    /* warmboot deinit */
    ret = ctc_warmboot_deinit(ldev);
    if (CTC_INIT_ERR_RET(ret))
    {
        CTC_INIT_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "warmboot_deinit failed:%s@%d %s\n",  __FUNCTION__, __LINE__, ctc_get_error_desc(ret));
        return ret;
    }

    return ret;
}

int32
ctcs_sdk_init(uint8 ldev, void * p_global_cfg)
{
    int32 ret = CTC_E_NONE;

    ret = ctcs_sdk_copy_global_cfg(ldev, (ctc_init_cfg_t*)p_global_cfg);
    if (ret != CTC_E_NONE)
    {
        return ret;
    }
    ret = _ctcs_sdk_init(ldev, p_global_cfg);
    if (ret != CTC_E_NONE)
    {
        ctcs_sdk_clear_global_cfg(ldev);
        return ret;
    }
    return CTC_E_NONE;
}

int32
ctcs_sdk_deinit(uint8 ldev)
{
    int32 ret = CTC_E_NONE;

    ret = _ctcs_sdk_deinit(ldev);
    if (ret != CTC_E_NONE)
    {
        return ret;
    }
    ctcs_sdk_clear_global_cfg(ldev);
    return CTC_E_NONE;
}
int32
ctcs_sdk_change_mem_profile(uint8 ldev, ctc_ftm_change_profile_t* p_profile)
{
#if 0
    int32  ret = 0;
    ctc_ftm_key_info_t* p_tmp_key_info;
    ctc_ftm_tbl_info_t* p_tmp_tbl_info;
    ctc_wb_api_t wb_api;
    bool val = FALSE;

    if (lchip >= CTC_MAX_LOCAL_DEV_NUM)
    {
        return CTC_E_INVALID_CHIP_ID;
    }
    CTC_PTR_VALID_CHECK(g_ctcs_init_cfg[lchip]);
    CTC_PTR_VALID_CHECK(p_profile);

    if(g_ctcs_init_cfg[lchip]->ftm_info.profile_type != CTC_FTM_PROFILE_USER_DEFINE)
    {
        return CTC_E_NOT_SUPPORT;
    }
    p_profile->old_profile = &g_ctcs_init_cfg[lchip]->ftm_info;
    CTC_ERROR_RETURN(ctcs_global_ctl_set(lchip, CTC_GLOBAL_NET_RX_EN, (void*)&val));
    CTC_ERROR_GOTO(ctcs_ftm_mem_change(lchip, p_profile), ret, end_0);

    /*copy new ftm profile to g_ctcs_init_cfg[lchip]*/
    p_tmp_tbl_info = g_ctcs_init_cfg[lchip]->ftm_info.tbl_info;
    p_tmp_key_info = g_ctcs_init_cfg[lchip]->ftm_info.key_info;
    sal_memcpy(&g_ctcs_init_cfg[lchip]->ftm_info, p_profile->new_profile, sizeof(ctc_ftm_profile_info_t));
    g_ctcs_init_cfg[lchip]->ftm_info.key_info = p_tmp_key_info;
    g_ctcs_init_cfg[lchip]->ftm_info.tbl_info = p_tmp_tbl_info;
    sal_memcpy(p_tmp_key_info, p_profile->new_profile->key_info, p_profile->new_profile->key_info_size*sizeof(ctc_ftm_key_info_t));
    sal_memcpy(p_tmp_tbl_info, p_profile->new_profile->tbl_info, p_profile->new_profile->tbl_info_size*sizeof(ctc_ftm_tbl_info_t));

    switch(p_profile->change_mode)
    {
        case CTC_FTM_MEM_CHANGE_REBOOT:/*only deinit/init*/
            CTC_ERROR_GOTO(_ctcs_sdk_deinit(lchip), ret, end_0);
            ctc_pmem_deinit(lchip);
            ctc_wb_deinit(lchip);
            sal_memset(&wb_api, 0, sizeof(wb_api));
            ctc_app_wb_init_api_t(lchip, 1, &wb_api);
            ctc_app_sdb_addr_init(lchip,
                                            g_ctcs_init_cfg[lchip]->p_chip_cfg->sdb_en,
                                            wb_api.start_addr,
                                            &g_ctcs_init_cfg[lchip]->p_chip_cfg->sdb_mem_addr,
                                            &g_ctcs_init_cfg[lchip]->p_chip_cfg->sdb_mem_size);
            wb_api.reloading = 0;
            CTC_ERROR_GOTO(ctc_wb_init(lchip, &wb_api), ret, end_0);
            CTC_ERROR_GOTO(_ctcs_sdk_init(lchip, g_ctcs_init_cfg[lchip]), ret, end_0);
            break;
        case CTC_FTM_MEM_CHANGE_RECOVER:/*recover data by warmboot db*/
            /*warmboot sync for lchip*/
            CTC_ERROR_GOTO(ctc_wb_sync(lchip), ret, end_0);
            CTC_ERROR_GOTO(ctc_wb_sync_done(lchip, 0), ret, end_0);

            /*SDK deinit*/
            CTC_ERROR_GOTO(_ctcs_sdk_deinit(lchip), ret, end_0);

            /*warmboot init for lchip*/
            ctc_pmem_deinit(lchip);
            ctc_wb_deinit(lchip);
            ctc_app_wb_init_api_t(lchip, 0, &wb_api);
            ctc_app_sdb_addr_init(lchip,
                                            g_ctcs_init_cfg[lchip]->p_chip_cfg->sdb_en,
                                            wb_api.start_addr,
                                            &g_ctcs_init_cfg[lchip]->p_chip_cfg->sdb_mem_addr,
                                            &g_ctcs_init_cfg[lchip]->p_chip_cfg->sdb_mem_size);
            wb_api.reloading = 1;
            CTC_ERROR_GOTO(ctc_wb_init(lchip, &wb_api), ret, end_0);
            /*SDK re-init*/
            CTC_ERROR_GOTO(_ctcs_sdk_init(lchip, g_ctcs_init_cfg[lchip]), ret, end_0);
            break;
        default:
            break;
    }
end_0:
    val = TRUE;
    ctcs_global_ctl_set(lchip, CTC_GLOBAL_NET_RX_EN, (void*)&val);
    return ret;
#endif
     return 0;
}
